/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.pingan.mobilegw.core.service.convert;

import com.alibaba.fastjson.JSON;
import com.pingan.mobilegw.common.dal.dataobject.MaServiceConfigDO;
import com.pingan.mobilegw.core.model.ServiceConfig;
import com.pingan.mobilegw.core.model.enums.ControlKey;
import com.pingan.mobilegw.core.model.enums.ServiceStatus;
import org.springframework.util.CollectionUtils;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 服务配置模型转换器
 * 
 * @author jiadao
 * @version $Id: ServiceConfigConvertor.java, v 0.1 2013-4-8 下午2:21:43 jiadao Exp $
 */
public class ServiceConfigConvertor {

    /**
     * 将DO装换为模型
     * 
     * @param configDO 服务配置DO
     * @return 服务配置模型
     */
    @SuppressWarnings("unchecked")
    public static ServiceConfig convertDoToModel(MaServiceConfigDO configDO) {
        ServiceConfig config = new ServiceConfig();
        config.setInterfaceType(configDO.getInterfaceType());
        config.setStatus(ServiceStatus.getStatusByCode(configDO.getIsopen()));
        config.setMethodName(configDO.getMethodName());
        config.setSysName(configDO.getSysName());
        config.setOperationType(configDO.getOperationType());

        Map<String, String> confParams = (Map<String, String>) JSON.parse(configDO.getConfParams());
        if (!CollectionUtils.isEmpty(confParams)) {
            Set<Entry<String, String>> entries = confParams.entrySet();
            for (Iterator<Entry<String, String>> ite = entries.iterator(); ite.hasNext();) {
                Entry<String, String> entry = ite.next();
                ControlKey controlKey = ControlKey.getByKey(entry.getKey());
                if (controlKey != null) {
                    config.addRules(controlKey, entry.getValue());
                }
            }
        }
        return config;

    }

}
